/*
 * SwingNativeTest.cpp  0.4.0 / Nov 5, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "SwingNativeTest.h"

#ifdef CUTE4J_TEST

// SwingNativeTest :: Protected Member Functions. ------------------------------

/*!
 * \brief Initializes Swing.
 */
void SwingNativeTest::swingInitialize()
{
  JNIEnv *env = NativeTest::env();
  if (env) {
    jclass swingClass = env->FindClass("com/cute4j/swing/Swing");
    if (swingClass) {
      jmethodID initializeMID = env->GetMethodID(swingClass, "initialize",
                                                 "()V");
      if (initializeMID) {
        if (JNIWrapper::callStaticVoidMethod(env, swingClass, initializeMID)) {
          return;
        }
      }
    }
  }
  failCondition("Swing initialization failed");
}

/*!
 * \brief Disposes Swing.
 *
 * \param quitOnLastWindowClosed if \c true exits main loop when last window is
 *                               closed
 *
 * \return loop exit code
 */
int SwingNativeTest::swingStartEventLoop(bool quitOnLastWindowClosed)
{
  JNIEnv *env = NativeTest::env();
  if (env) {
    jclass swingClass = env->FindClass("com/cute4j/swing/Swing");
    if (swingClass) {
      jmethodID startEventLoopMID = env->GetMethodID(swingClass,
                                                     "startEventLoop", "(Z)I");
      if (startEventLoopMID) {
        jint result;
        if (JNIWrapper::callStaticIntMethod(env, result, swingClass,
                                            startEventLoopMID,
                                            quitOnLastWindowClosed)) {
          return result;
        }
      }
    }
  }
  failCondition("Could not start Swing event loop");
  return -1;
}

/*!
 * \brief Exits Swing event loop.
 *
 * \param extiCode the loop exit code
 */
void SwingNativeTest::swingStopEventLoop(int extiCode)
{
  JNIEnv *env = NativeTest::env();
  if (env) {
    jclass swingClass = env->FindClass("com/cute4j/swing/Swing");
    if (swingClass) {
      jmethodID stopEventLoopMID = env->GetMethodID(swingClass,
                                                    "stopEventLoopMID",
                                                    "(I)V");
      if (stopEventLoopMID) {
        if (JNIWrapper::callStaticVoidMethod(env, swingClass, stopEventLoopMID,
                                             extiCode)) {
          return;
        }
      }
    }
  }
  failCondition("Could not stop Swing event loop");
}

/*!
 * \brief Disposes Swing.
 */
void SwingNativeTest::swingDispose()
{
  JNIEnv *env = NativeTest::env();
  if (env) {
    jclass swingClass = env->FindClass("com/cute4j/swing/Swing");
    if (swingClass) {
      jmethodID disposeMID = env->GetMethodID(swingClass, "dispose", "()V");
      if (disposeMID) {
        if (JNIWrapper::callStaticVoidMethod(env, swingClass, disposeMID)) {
          return;
        }
      }
    }
  }
  failCondition("Could not dispose Swing");
}

#endif // CUTE4J_TEST
